package com.siysoft.tc.friend.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.siysoft.tc.base.bean.bo.ClassroomShortBo;
import com.siysoft.tc.base.bean.bo.FriendBo;
import com.siysoft.tc.base.bean.bo.PaginationBo;
import com.siysoft.tc.base.bean.property.ResourceType;
import com.siysoft.tc.base.bean.to.Friend;
import com.siysoft.tc.base.bean.to.Resource;
import com.siysoft.tc.base.bean.to.User;
import com.siysoft.tc.base.bean.to.VUserFriend;
import com.siysoft.tc.friend.service.IFriendService;
import com.siysoft.util.COMMON;
import com.tc.base.dao.BaseDAO;

@Service
public class FriendServiceImpl implements IFriendService{
	@Autowired
	BaseDAO baseDao;
	
	public String getResource(int data_id, String type) {
		String hql= "from Resource a where a.dataType='"+type+"' AND a.dataId="+data_id;
		List<Resource> resourcelist =baseDao.findByHQL(hql);
		Resource resource1=new Resource();
		if(resourcelist.size()!=0){
			return resourcelist.get(0).getImgPath();
		}
		return null;
		
	}

	@Override
	public List<FriendBo> getfriend(Integer id, PaginationBo pb) {
		// TODO Auto-generated method stub
		try{
			String hql = "from VUserFriend where friend_head_id="+id;
			List<VUserFriend> lvuf = (List<VUserFriend>)baseDao.findByHQL(hql, null, pb.getPagination_offset(), pb.getPagination_limit());
			pb.setPagination_total(lvuf.size());
			List<FriendBo> lfb = new ArrayList<FriendBo>();
			System.out.println(lvuf.size());
			for(Integer i = 0; i < lvuf.size(); i++){
				VUserFriend f = lvuf.get(i);
				Integer friend_id = f.getFriend_dependent_id();
				String  friend_name = f.getFriend_dependent_name();
				String friend_avatar_url = COMMON.NulltoAvatar(this.getResource(friend_id,ResourceType.USER_AVATAR));;
				FriendBo lf = new FriendBo(friend_id, friend_name,
						friend_avatar_url);
				lfb.add(lf);
			}
			return lfb;
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}

	@Override
	public String delete(Integer head_id, Integer dependent_id) {
		// TODO Auto-generated method stub
		String hql = "from Friend where friend_head_id="+head_id+" and friend_dependent_id="+dependent_id;
		List<Friend> lf = baseDao.findByHQL(hql);
		if(lf.size() > 0){
			baseDao.delete(lf.get(0));
			return "Success for delete!";
		}
		else{
			return "They are not friend!";
		}
	}

	@Override
	public List<FriendBo> searchUser(String name, PaginationBo pb) {
		// TODO Auto-generated method stub
		String hql = "from User where user_name='"+name+"'";
		List<User> lu = baseDao.findByHQL(hql);
		List<FriendBo> lfb = new ArrayList<FriendBo>();
		if(COMMON.isEmpty(lu)){
			return null;
		}
		else{
			for(Integer i = 0; i < lu.size(); i++){
				User u = lu.get(i);
				int friend_id = u.getUser_id();
				String friend_name = u.getUser_name();
				String friend_avatar_url = COMMON.NulltoAvatar(this.getResource(friend_id,ResourceType.USER_AVATAR));;
				FriendBo fb = new FriendBo(friend_id, friend_name, friend_avatar_url);
				lfb.add(fb);
			}
			return lfb;
		}
	}

	@Override
	public String add(Integer head_id, Integer dependent_id) {
		// TODO Auto-generated method stub
		String hql = "from Friend where friend_head_id="+head_id+" and friend_dependent_id="+dependent_id;
		List<Friend> lf = baseDao.findByHQL(hql);
		if(lf.size() > 0){
			return "They have been friends!";
		}
		else{
			Friend f = new Friend();
			f.setFriend_head_id(head_id);
			f.setFriend_dependent_id(dependent_id);
			baseDao.save(f);
			return "Success for add friend!";
		}
	}

	@Override
	public List<ClassroomShortBo> getClassroomShort(Integer id, PaginationBo pb) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ClassroomShortBo> searchClassroom(String number, PaginationBo pb) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer invite(Integer id, String number) {
		// TODO Auto-generated method stub
		return null;
	}

}
